package cn.ycc1.mymiddle.rag;

import org.springframework.ai.document.Document;
import org.springframework.ai.rag.Query;
import org.springframework.ai.rag.retrieval.search.DocumentRetriever;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 文档检索
 * @author ycc
 * @date 2025/5/1
 * https://java2ai.com/docs/1.0.0-M6.1/tutorials/retriever/?spm=4347728f.15fb5545.0.0.66256001vA5irw
 * 文档检索（DocumentRetriever）是一种信息检索技术，旨在从大量未结构化或半结构化文档中快速找到与特定查询相关的文档或信息。
 * 文档检索通常以在线(online)方式运行。
 * 创建知识库索引：
 * https://help.aliyun.com/zh/model-studio/api-bailian-2023-12-29-createindex?spm=a2c4g.11186623.0.i3
 * https://help.aliyun.com/zh/model-studio/retrieval-augmented-generation?scm=20140722.S_help%40%40%E6%96%87%E6%A1%A3%40%402846851.S_BB2%40bl%2BRQW%40ag0%2BBB1%40ag0%2Bos0.ID_2846851-RL_%E6%8E%A7%E5%88%B6%E5%8F%B0%E5%88%9B%E5%BB%BA%E7%9F%A5%E8%AF%86%E5%BA%93-LOC_doc%7EUND%7Eab-OR_ser-PAR1_2102029b17461020515822792d256d-V_4-P0_0-P1_0&spm=a2c4g.11186623.help-search.i62
 * https://help.aliyun.com/zh/model-studio/rag-knowledge-base?spm=a2c4g.11186623.0.0.e16b472fSydQqF
 *
 * dashscope控制台创建知识库
 * https://bailian.console.aliyun.com/?tab=app#/knowledge-base
 */
@RestController
public class RetrieverController {
    private final DocumentRetriever documentRetriever;

    @Autowired
    public RetrieverController(DocumentRetriever documentRetriever) {
        this.documentRetriever = documentRetriever;
    }

    @GetMapping("/ai/retrieve")
    public List<Document> retrieve(@RequestParam(value = "message", defaultValue = "What's spring ai") String message) {
        return this.documentRetriever.retrieve(new Query(message));
    }
}
